package com.wushixin.springdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wushixin.springdemo.dto.UserDto;
import com.wushixin.springdemo.pojo.Member;
import com.wushixin.springdemo.pojo.User;
import com.wushixin.springdemo.pojo.utils.Result;
import com.wushixin.springdemo.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;


    @GetMapping
    public Result GetAll() {
        return  new Result(memberService.list(),memberService.list()!=null?true:false);
    }
    @GetMapping("{id}")
    public Result GetById(@PathVariable Integer id) {
        return new Result(memberService.getById(id),true);
    }

    @PostMapping
    public Result saveStudent(@RequestBody Member member) {
        member.setRegdate(new Date());
        return new Result(null,memberService.save(member));
    }

    @DeleteMapping("{id}")
    public Result deleteStudent(@PathVariable Integer id) {
        boolean flag = memberService.removeById(id);
        return new Result(null,flag);
    }

    @PutMapping
    public Result updateStudent(@RequestBody Member member) {
        Boolean flag = memberService.updateById(member);

        return new Result(flag,flag?"添加成功":"添加失败");
    }

    @GetMapping("{current}/{pageSize}")
    public Result pageStudent(@PathVariable Integer current,@PathVariable Integer pageSize, Member member) {

        Page<Member> page = new Page<Member>(current,pageSize);
        if (member.getName() != null || member.getPwd() != null || member.getBalance() != null) {
            System.out.println(" ============= 参数为不空 ============= ");
            val memberLambdaQueryWrapper = new LambdaQueryWrapper<Member>();
            memberLambdaQueryWrapper.like(member.getName()!=null,Member::getName,member.getName());
            memberService.page(page,memberLambdaQueryWrapper);
            if(current > page.getPages()) {
                page = memberService.page(page,memberLambdaQueryWrapper);;
            }

        }else {
            System.out.println(" ============= 参数为空 ============= ");
            page = memberService.page(page);
            if(current > page.getPages()) {
                page = memberService.page(page);
            }
        }

        return new Result(page,page != null ? true:false);
    }
}
